package com.apes.fn.fico.expense;

import com.apes.fn.business.insurance.modle.InsuranceOrder;
import com.apes.fn.business.insurance.modle.PolicyInformation;
import com.apes.fn.business.insurance.repository.PolicyInformationRepository;
import com.apes.fn.fico.settleExpense.SettleExpense;
import com.apes.fn.scm.advancePayment.AdvancePayment;
import com.apes.fn.scm.retailOrder.model.RetailOrder;
import com.apes.fn.scm.used.model.UsedCar;
import com.apes.fn.scm.vipSettle.model.VipUnionSettle;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.outbox.annotation.OutboxSubscriber;
import com.apes.framework.util.DateUtil;
import com.apes.framework.util.MapUtil;
import com.apes.scm.income.model.MoneyIncome;
import com.apes.scm.voucher.model.Voucher;
import com.apes.scm.voucher.model.VoucherFinExtension;
import com.apes.scm.voucher.repository.VoucherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class ExpenseSubscriberService {

    @Autowired
    private ExpenseService expenseService;
    @Autowired
    private ExpenseItemDetailsBillRepository expenseItemDetailsBillRepository;
    @Autowired
    private VoucherRepository voucherRepository;
    @Autowired
    private PolicyInformationRepository policyInformationRepository;

    public static final Date DATE = DateUtil.parse("2023-02-09", DateUtil.SHOW_DATE_FORMAT);

    @OutboxSubscriber(id = "fn_expense_fee_advancePayment", name = "定金单写入平台费用", condition = "state == 'approve' && calculateFlag")
    public void advancePayment(AdvancePayment advancePayment) {
        if(DATE.after(advancePayment.getCreateDate())) return;
        if (expenseItemDetailsBillRepository.findByOrderId(advancePayment.getId()) > 0) return;
        Map<String, Object> m = MapUtil.mapped(advancePayment);
        m.put("ORDER_TYPE", "AdvancePayment");
        SimpleRequest simpleRequest = new SimpleRequest();
        simpleRequest.setData(m);
        expenseService.createPlatformFee(simpleRequest);
    }

    @OutboxSubscriber(id = "fn_expense_fee_insuranceOrder", name = "车险订单写入平台费用", condition = "state =='6' || state =='7' || state =='8'")
    public void insuranceOrder(InsuranceOrder insuranceOrder) {
        if(DATE.after(insuranceOrder.getCreateDate())) return;
        if (expenseItemDetailsBillRepository.findByOrderId(insuranceOrder.getId()) > 0) return;
        Map<String, Object> m = MapUtil.mapped(insuranceOrder);
        m.put("ORDER_TYPE", "InsuranceOrder");
        SimpleRequest simpleRequest = new SimpleRequest();
        simpleRequest.setData(m);
        expenseService.createPlatformFee(simpleRequest);
    }

    @OutboxSubscriber(id = "fn_expense_fee_moneyIncome", name = "资金收入单写入平台费用", condition = "state == 'approve' && calculateFlag")
    public void moneyIncome(MoneyIncome moneyIncome) {
        if(DATE.after(moneyIncome.getApproveDate())) return;
        if (expenseItemDetailsBillRepository.findByOrderId(moneyIncome.getId()) > 0) return;
        Map<String, Object> m = MapUtil.mapped(moneyIncome);
        m.put("ORDER_TYPE", "MoneyIncome");
        SimpleRequest simpleRequest = new SimpleRequest();
        simpleRequest.setData(m);
        expenseService.createPlatformFee(simpleRequest);
    }

    @OutboxSubscriber(id = "fn_expense_fee_policyInformation", name = "保单写入平台费用")
    public void policyInformation(PolicyInformation policyInformation) {
        PolicyInformation information = policyInformationRepository.findOne(policyInformation.getId());
        if(DATE.after(information.getCreateDate())) return;
        if (expenseItemDetailsBillRepository.findByOrderId(information.getId()) > 0) return;
        Map<String, Object> m = MapUtil.mapped(information);
        m.put("ORDER_TYPE", "PolicyInformation");
        SimpleRequest simpleRequest = new SimpleRequest();
        simpleRequest.setData(m);
        expenseService.createPlatformFee(simpleRequest);
    }

    @OutboxSubscriber(id = "fn_expense_fee_retailOrder", name = "零售单写入平台费用", condition = "calculateFlag")
    public void retailOrder(RetailOrder retailOrder) {
        if(DATE.after(retailOrder.getDateApprove())) return;
        if (expenseItemDetailsBillRepository.findByOrderId(retailOrder.getId()) > 0) return;
        Map<String, Object> m = MapUtil.mapped(retailOrder);
        m.put("ORDER_TYPE", "RetailOrder");
        SimpleRequest simpleRequest = new SimpleRequest();
        simpleRequest.setData(m);
        expenseService.createPlatformFee(simpleRequest);
    }

    @OutboxSubscriber(id = "fn_expense_fee_settleExpense", name = "平台定损费用单写入平台费用")
    public void settleExpense(SettleExpense settleExpense) {
        if(DATE.after(settleExpense.getCreateDate())) return;
        if (expenseItemDetailsBillRepository.findByOrderId(settleExpense.getId()) > 0) return;
        Map<String, Object> m = MapUtil.mapped(settleExpense);
        m.put("ORDER_TYPE", "SettleExpense");
        SimpleRequest simpleRequest = new SimpleRequest();
        simpleRequest.setData(m);
        expenseService.createPlatformFee(simpleRequest);
    }

    @OutboxSubscriber(id = "fn_expense_fee_usedCar", name = "二手车写入平台费用", condition = "state == 'approve' && calculateFlag")
    public void usedCar(UsedCar usedCar) {
        if(DATE.after(usedCar.getDateApprove())) return;
        if (expenseItemDetailsBillRepository.findByOrderId(usedCar.getId()) > 0) return;
        Map<String, Object> m = MapUtil.mapped(usedCar);
        m.put("ORDER_TYPE", "UsedCar");
        SimpleRequest simpleRequest = new SimpleRequest();
        simpleRequest.setData(m);

        expenseService.createPlatformFee(simpleRequest);
    }

    @OutboxSubscriber(id = "fn_expense_fee_vipUnionSettle", name = "会员联盟写入平台费用", condition = "state == 'done'")
    public void vipUnionSettle(VipUnionSettle vipUnionSettle) {
        if(DATE.after(vipUnionSettle.getCreateDate())) return;
        if (expenseItemDetailsBillRepository.findByOrderId(vipUnionSettle.getId()) > 0) return;
        Map<String, Object> m = MapUtil.mapped(vipUnionSettle);
        m.put("ORDER_TYPE", "VipUnionSettle");
        SimpleRequest simpleRequest = new SimpleRequest();
        simpleRequest.setData(m);
        expenseService.createPlatformFee(simpleRequest);
    }

    @OutboxSubscriber(id = "fn_expense_fee_voucher", name = "业务凭证写入平台费用")
    public void voucher(VoucherFinExtension voucherFinExtension) {
        if(DATE.after(voucherFinExtension.getVoucher().getCreateDate())) return;
        if (expenseItemDetailsBillRepository.findByOrderId(voucherFinExtension.getId()) > 0) return;
        Voucher voucher = voucherRepository.findOne(voucherFinExtension.getId());
        Map<String, Object> m = MapUtil.mapped(voucher);
        m.put("ORDER_TYPE", "Voucher");
        SimpleRequest simpleRequest = new SimpleRequest();
        simpleRequest.setData(m);
        expenseService.createPlatformFee(simpleRequest);
    }



}
